'use strict';

/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * 初始化国际化
 * zengbinsi
 * 2020-08-26
 */


import path from 'path';
import { exec } from 'child_process';
import pathUtil from '../libs/utils/path-utils';
import fileUtil from '../libs/utils/file-utils';
import { callSequence } from '../libs/utils/function-call-seq';
const srcPath = pathUtil.getPathInTemplateAssets(path.join('bundles', 'framework', 'ui', 'i18n'));
const dstPath = pathUtil.getPathInAssets(path.join('bundles', 'framework', 'ui'));

// 初始化目录
const initDir = (callback?: Function) => {
    pathUtil.mkdirsSyncInAssets(path.join('bundles', 'framework', 'ui'), callback);
};


export const importI18n = (callback?: Function)=>{
    console.log('[tool-box]import i18n...');

    if (fileUtil.existsSync(path.join(dstPath, 'i18n'))) {
        return console.log('[tool-box]i18n is already imported!');
    }

    return callSequence([initDir, (cb?: Function) => {
        const sh = `cp -rf ${srcPath} ${dstPath}`;
        exec(sh, async(err, stdout, stderr) => {
            if (err || stderr) { return console.log('[tool-box]import i18n failed', err, stderr); }

            fileUtil.rename(path.join(dstPath, 'i18n', 'GGI18nManager.tsz'), path.join(dstPath, 'i18n', 'GGI18nManager.ts'));
            fileUtil.rename(path.join(dstPath, 'i18n', 'GGI18nLabel.tsz'), path.join(dstPath, 'i18n', 'GGI18nLabel.ts'));
            fileUtil.rename(path.join(dstPath, 'i18n', 'GGI18nSprite.tsz'), path.join(dstPath, 'i18n', 'GGI18nSprite.ts'));
            fileUtil.rename(path.join(dstPath, 'i18n', 'GGI18nRichText.tsz'), path.join(dstPath, 'i18n', 'GGI18nRichText.ts'));
            await pathUtil.refreshAsset(`db://assets/bundles/framework/ui/i18n`);
            console.log('[tool-box]', 'import i18n successed!');
            cb && cb();
        });
    }, callback]);
};

